//
//  MCHistoryCollectionView.m
//  ZXZX
//
//  Created by menhao on 2018/6/5.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCHistoryCollectionView.h"
#import "MCHomeHistroyCollectionViewCell.h"

@interface MCHistoryCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *ballLayout;
@end
@implementation MCHistoryCollectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      [self setUpUI];
    }
    return self;
}


- (void)setUpUI{
    
    UICollectionViewFlowLayout *ballLayout = [[UICollectionViewFlowLayout alloc] init];
    ballLayout.minimumLineSpacing = MC_REALVALUE(2);
    ballLayout.minimumInteritemSpacing = MC_REALVALUE(0);
    ballLayout.itemSize = CGSizeMake(MC_REALVALUE(16), MC_REALVALUE(16));
    ballLayout.sectionInset = UIEdgeInsetsMake(MC_REALVALUE(6), 0, 0, 0);
    self.ballLayout = ballLayout;
    UICollectionView *ballColletionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:ballLayout];
    [ballColletionView registerNib:[UINib nibWithNibName:NSStringFromClass([MCHomeHistroyCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"MCHomeHistroyCollectionViewCell"];
    ballColletionView.delegate = self;
    ballColletionView.dataSource = self;
    ballColletionView.scrollEnabled = NO;
    [self addSubview:ballColletionView];
    ballColletionView.backgroundColor = [UIColor whiteColor];
    self.collectionView = ballColletionView;
}

- (void)setLastHistoryBallArray:(NSArray *)lastHistoryBallArray{
    _lastHistoryBallArray = lastHistoryBallArray;
    
    if (lastHistoryBallArray.count <= 10) {
        self.ballLayout.sectionInset = UIEdgeInsetsMake(MC_REALVALUE(0), 0, 0, 0);
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.centerX.equalTo(self);
            make.height.equalTo(@(MC_REALVALUE(16)));
            make.width.equalTo(@(MC_REALVALUE(17) * lastHistoryBallArray.count));
        }];
    }else{
        self.ballLayout.sectionInset = UIEdgeInsetsMake(MC_REALVALUE(6), 0, 0, 0);
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    [self.collectionView reloadData];
    
}
#pragma mark -collectionView datasource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.lastHistoryBallArray.count>20) {
        return 20;
    }
    return self.lastHistoryBallArray.count;
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MCHomeHistroyCollectionViewCell *cell = nil;
    NSInteger index = indexPath.row;
   
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MCHomeHistroyCollectionViewCell" forIndexPath:indexPath];
    
    if (index==0) {
        cell.numberLabel.backgroundColor=RGB(107, 117, 240);
    }else if (index==1){
        cell.numberLabel.backgroundColor=RGB(34, 174, 232);
    }else if (index==2){
        cell.numberLabel.backgroundColor=RGB(0, 214, 197);
    }else if (index==3){
        cell.numberLabel.backgroundColor=RGB(0, 215, 48);
    }else if (index==4){
        cell.numberLabel.backgroundColor=RGB(183, 216, 52);
    }else if (index==5){
        cell.numberLabel.backgroundColor=RGB(255, 187, 51);
    }else if (index==6){
        cell.numberLabel.backgroundColor=RGB(255, 109, 36);
    }else if (index==7){
        cell.numberLabel.backgroundColor=RGB(255, 43, 51);
    }else if (index==8){
        cell.numberLabel.backgroundColor=RGB(255, 40, 136);
    }else if (index==9){
        cell.numberLabel.backgroundColor=RGB(242, 32, 250);
    } else if (index==10) {
        cell.numberLabel.backgroundColor=RGB(107, 117, 240);
    }else if (index==11){
        cell.numberLabel.backgroundColor=RGB(34, 174, 232);
    }else if (index==12){
        cell.numberLabel.backgroundColor=RGB(0, 214, 197);
    }else if (index==13){
        cell.numberLabel.backgroundColor=RGB(0, 215, 48);
    }else if (index==14){
        cell.numberLabel.backgroundColor=RGB(183, 216, 52);
    }else if (index==15){
        cell.numberLabel.backgroundColor=RGB(255, 187, 51);
    }else if (index==16){
        cell.numberLabel.backgroundColor=RGB(255, 109, 36);
    }else if (index==17){
        cell.numberLabel.backgroundColor=RGB(255, 43, 51);
    }else if (index==18){
        cell.numberLabel.backgroundColor=RGB(255, 40, 136);
    }else if (index==19){
        cell.numberLabel.backgroundColor=RGB(242, 32, 250);
    }
    cell.numberLabel.text = self.lastHistoryBallArray[indexPath.row];
    return cell;
}


@end
